package com.dingwei.superiordiary.ui.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.PersonalBean;
import com.dingwei.superiordiary.bean.Upload_Head_Image_Bean;
import com.dingwei.superiordiary.event.MessageEvent;
import com.dingwei.superiordiary.mvp.presenter.PersonalDataPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.utils.DialogUtil;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.L;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lihao
 * @date 2018/1/11
 */

public class PersonalDataActivity extends BaseActivity implements Contract.PersonalDataView {
    PersonalDataPresenter personalDataPresenter = new PersonalDataPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.ll_img_head)
    LinearLayout llImgHead;
    @Bind(R.id.tv_nick_name)
    TextView tvNickName;
    @Bind(R.id.ll_nick_name)
    LinearLayout llNickName;
    @Bind(R.id.tv_six)
    TextView tvSix;
    @Bind(R.id.ll_six)
    LinearLayout llSix;
    @Bind(R.id.tv_age)
    TextView tvAge;
    @Bind(R.id.ll_age)
    LinearLayout llAge;
    @Bind(R.id.tv_idiograph)
    TextView tvIdiograph;
    @Bind(R.id.ll_idiograph)
    LinearLayout llIdiograph;
    @Bind(R.id.data_layout)
    LinearLayout dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    private UMShareAPI mShareAPI;
    public String lastPath = "";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_personal;
    }

    @Override
    protected void initData() {
        personalDataPresenter.getUserData();
    }

    @Override
    protected void initView() {
        tvTitle.setText("个人资料");

        mShareAPI = UMShareAPI.get(context);
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        UMShareAPI.get(PersonalDataActivity.this).setShareConfig(config);
    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.img_back, R.id.ll_img_head, R.id.ll_nick_name, R.id.ll_idiograph,
            R.id.ll_six, R.id.ll_age, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_img_head:
                PictureSelector.create(PersonalDataActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .theme(R.style.picture_white_style)
                        .imageSpanCount(3)
                        .selectionMode(PictureConfig.SINGLE)
                        .previewImage(true)
                        .isCamera(true)
                        .isZoomAnim(true)
                        .enableCrop(true)// 是否裁剪 true or false
                        .compress(true)// 是否压缩 true or false
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽 true or false
                        .circleDimmedLayer(true)// 是否圆形裁剪 true or false
                        .showCropFrame(false)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                        .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
                        .rotateEnabled(true) // 裁剪是否可旋转图片 true or false
                        .scaleEnabled(true)// 裁剪是否可放大缩小图片 true or false
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.ll_nick_name:
                intent = new Intent(BaseActivity.context, ModifyNickNameActivity.class);
                intent.putExtra("nickname", tvNickName.getText().toString());
                startActivityForResult(intent, 0);
                break;
            case R.id.ll_idiograph:
                intent = new Intent(BaseActivity.context, ModifyIdiographActivity.class);
                intent.putExtra("idiograph", tvIdiograph.getText().toString());
                startActivityForResult(intent, 7);
                break;
            case R.id.ll_six:
                personalDataPresenter.showChooseSexDialog(this, tvSix.getText().toString());
                break;
            case R.id.ll_age:
                intent = new Intent(BaseActivity.context, ModifyNickAgeActivity.class);
                intent.putExtra("age", tvAge.getText().toString());
                startActivityForResult(intent, 1);
                break;
            case R.id.no_net_btn:
                personalDataPresenter.getUserData();
                break;
        }
    }

//    private UMAuthListener umAuthListener = new UMAuthListener() {
//        @Override
//        public void onStart(SHARE_MEDIA platform) {
//            //授权开始的回调
//            dialog.show();
//        }
//
//        @Override
//        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
//            L.e(platform.toString());
//            personalDataPresenter.WXlogin(platform.toString(), data.get("uid"));
//            dialog.dismiss();
//
//        }
//
//        @Override
//        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
//            Toast.makeText(getApplicationContext(), "授权失败", Toast.LENGTH_SHORT).show();
//            dialog.dismiss();
//        }
//
//        @Override
//        public void onCancel(SHARE_MEDIA platform, int action) {
//            Toast.makeText(getApplicationContext(), "取消授权", Toast.LENGTH_SHORT).show();
//            dialog.dismiss();
//        }
//    };

    @Override
    public void setUserData(PersonalBean bean) {
        tvNickName.setText(bean.data.nickname);
        switch (bean.data.gender){
            case 1:
                tvSix.setText("保密");
                break;
            case 2:
                tvSix.setText("男");
                break;
            case 3:
                tvSix.setText("女");
                break;
        }
        tvIdiograph.setText(bean.data.idiograph);

        tvAge.setText(bean.data.age);
        Glide.with(BaseActivity.context)
                .load(bean.data.portrait)
                .apply(MyApplication.headOptions)
                .into(imgHead);
//        if (bean.data.bind_qq == 2) {
//            tvQq.setText("已绑定");
//        } else {
//            tvQq.setText("未绑定");
//        }
//        if (bean.data.bind_wx == 2) {
//            tvWechat.setText("已绑定");
//        } else {
//            tvWechat.setText("未绑定");
//        }
        showData("success");
    }

    @Override
    public void uploadPicSuccess(Upload_Head_Image_Bean bean) {
        Glide.with(BaseActivity.context)
                .load(bean.data)
                .apply(MyApplication.headOptions)
                .into(imgHead);
        PictureFileUtils.deleteCacheDirFile(PersonalDataActivity.this);
    }

    @Override
    public void ChooseSex(String gender) {
        personalDataPresenter.saveGender(gender);
    }

    @Override
    public void saveGenderSuccess(String gender) {
        tvSix.setText(gender);
    }

    @Override
    public void Fail() {
        showData("fail");
    }

//    @Override
//    public void bindQQSuccess() {
//        tvQq.setText("已绑定");
//    }
//
//    @Override
//    public void bindWXSuccess() {
//        tvWechat.setText("已绑定");
//    }
//
//    @Override
//    public void unbindQQSuccess() {
//        tvQq.setText("未绑定");
//    }
//
//    @Override
//    public void unbindWXSuccess() {
//        tvWechat.setText("未绑定");
//    }

    private void showData(String state) {
        if (state.equals("success")) {
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case 0:
                tvNickName.setText(data.getStringExtra("nickname"));
                break;
            case 1:
                tvAge.setText(data.getStringExtra("age"));
                break;
            case 7 :
                tvIdiograph.setText(data.getStringExtra("idiograph"));
                EventBus.getDefault().post(new MessageEvent(data.getStringExtra("idiograph")));
                break;
            case PictureConfig.CHOOSE_REQUEST:
                // 图片选择结果回调
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                // 例如 LocalMedia 里面返回三种path
                // 1.media.getPath(); 为原图path
                // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                lastPath = selectList.get(0).getCompressPath();
                personalDataPresenter.uploadHeadImg(lastPath);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
